// pages/index/allBrands/allBrands.js
import config from "../../../utils/config.js";
Page({
  /**
   * Page initial data
   */
  data: {
    all: [],//show this list
    catList: {
      capA: {name:"A",data:[]},
      capB: {name:"B",data:[]},
      capC: {name:"C",data:[]},
      capD: {name:"D",data:[]},
      capE: {name:"E",data:[]},
      capF: {name:"F",data:[]},
      capG: {name:"G",data:[]},
      capH: {name:"H",data:[]},
      capI: {name:"I",data:[]},
      capJ: {name:"J",data:[]},
      capK: {name:"K",data:[]},
      capL: {name:"L",data:[]},
      capM: {name:"M",data:[]},
      capN: {name:"N",data:[]},
      capO: {name:"O",data:[]},
      capP: {name:"P",data:[]},
      capQ: {name:"Q",data:[]},
      capR: {name:"R",data:[]},
      capS: {name:"S",data:[]},
      capT: {name:"T",data:[]},
      capU: {name:"U",data:[]},
      capV: {name:"V",data:[]},
      capW: {name:"W",data:[]},
      capX: {name:"X",data:[]},
      capY: {name:"Y",data:[]},
      capZ: {name:"Z",data:[]},
      capOth: {name:"#",data:[]},
    },
  },
  // cat by cap
  cat: function(up,low) {
    var size = this.data.all.length;
    var arr = [];
    for(var i=0;i<size;i++) {
      if (this.data.all[i].name[0] == up || this.data.all[i].name[0] == low) {
        arr.push(this.data.all[i]);
      }
    }
    return arr;
  },
  // cat not letter
  catNotLetter: function () {
    var size = this.data.all.length;
    var arr = [];
    var reg = /^[A-Za-z]$/;
    for (var i = 0; i < size; i++) {
      if (!reg.test(this.data.all[i].name[0])) {
        arr.push(this.data.all[i]);
      }
    }
    return arr;
  },
  // cat all 
  catAll: function() {
    this.setData({
      "catList.capA.data": this.cat("A", "a"),
      "catList.capB.data": this.cat("B", "b"),
      "catList.capC.data": this.cat("C", "c"),
      "catList.capD.data": this.cat("D", "d"),
      "catList.capE.data": this.cat("E", "e"),
      "catList.capF.data": this.cat("F", "f"),
      "catList.capG.data": this.cat("G", "g"),
      "catList.capH.data": this.cat("H", "h"),
      "catList.capI.data": this.cat("I", "i"),
      "catList.capJ.data": this.cat("J", "j"),
      "catList.capK.data": this.cat("K", "k"),
      "catList.capL.data": this.cat("L", "l"),
      "catList.capM.data": this.cat("M", "m"),
      "catList.capN.data": this.cat("N", "n"),
      "catList.capO.data": this.cat("O", "o"),
      "catList.capP.data": this.cat("P", "p"),
      "catList.capQ.data": this.cat("Q", "q"),
      "catList.capR.data": this.cat("R", "r"),
      "catList.capS.data": this.cat("S", "s"),
      "catList.capT.data": this.cat("T", "t"),
      "catList.capU.data": this.cat("U", "u"),
      "catList.capV.data": this.cat("V", "v"),
      "catList.capW.data": this.cat("W", "w"),
      "catList.capX.data": this.cat("X", "x"),
      "catList.capY.data": this.cat("Y", "y"),
      "catList.capZ.data": this.cat("Z", "z"),
      "catList.capOth.data": this.catNotLetter(),
    })
  },
  // show brand details
  showBrandDetails: function(e) {
    wx.navigateTo({
      url: '../brandDetails/brandDetails?bid=' + e.currentTarget.dataset.brandid,
    })
  },
  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '品牌街',
    });
    wx.request({
      url: config.brand,
      data: {size:100,page:1},
      success: function(res) {
        // console.log(res.data.data);
        that.setData({
          all: res.data.data.data
        });
        that.catAll();
        // console.log(that.data.catList);
      }
    })
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function () {
    
  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function () {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function () {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function () {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function () {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function () {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function () {

  }
})